package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.SysUserDto;
import com.plz.afmt.dto.SysUserQueryDto;
import com.plz.afmt.entity.SysUserEntity;
import com.plz.afmt.mapper.SysUserMapper;
import com.plz.afmt.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.SysUserTransfer;
import com.plz.afmt.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 系统用户
 *
 * @author jzc
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {
    @Autowired
    private SysUserTransfer sysUserTransfer;

    @Override
    public PagerResult<SysUserVo> pageQuery(SysUserQueryDto dto) {
        QueryWrapper<SysUserEntity> queryWrapper = PageCommonUtil.dtoToWrapper(SysUserEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<SysUserEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<SysUserVo> voPagerResult = sysUserTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public SysUserEntity addOrUpdate(SysUserDto dto) {
            SysUserEntity entity = new SysUserEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<SysUserEntity> addOrUpdateList(List<SysUserDto> dto) {
        List<SysUserEntity> entitys = sysUserTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<SysUserVo> listFor(SysUserQueryDto dto) {
        QueryWrapper<SysUserEntity> queryWrapper = PageCommonUtil.dtoToWrapper(SysUserEntity.class, dto);
        List<SysUserEntity> list = this.list(queryWrapper);
        List<SysUserVo> vos = sysUserTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public SysUserVo detail(Long id) {
            SysUserEntity entity = this.getById(id);
            SysUserVo vo = new SysUserVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public SysUserEntity queryByLoginName(String loginName) {
        return this.getOne(new LambdaQueryWrapper<SysUserEntity>()
                .eq(SysUserEntity::getLoginName,loginName));
    }

}



